package cn.zyy.hotel.controller.admin;

import cn.zyy.hotel.entity.Room;
import cn.zyy.hotel.service.RoomService;
import cn.zyy.hotel.utils.Result;
import cn.zyy.hotel.utils.SystemConstant;
import cn.zyy.hotel.vo.RoomVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/room")
public class RoomAdminController {

    @Resource
    private RoomService roomService;

    //查询房间列表
    @RequestMapping("/list")
    public Result list(RoomVO roomVo){
        //设置分页信息
        PageHelper.startPage(roomVo.getPage(),roomVo.getLimit());
        //调用查询房间列表的方法
        List<Room> roomList = roomService.selectRoomListByPage(roomVo);
        //创建分页对象
        PageInfo<Room> pageInfo = new PageInfo<Room>(roomList);
        return Result.builder().code(0).count(pageInfo.getTotal()).data(pageInfo.getList()).build();
    }

    //添加房间
    @RequiresPermissions("room:add")
    @RequestMapping("/addRoom")
    public Object addRoom(Room room){
        Map<String, Object> map = new HashMap<String, Object>();
        if (roomService.addRoom(room)>0){
            //成功
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"添加成功");
        }else {
            //失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"添加失败");
        }
        return map;
    }

    //修改房间
    @RequiresPermissions("room:edit")
    @RequestMapping("/updateRoom")
    public Object updateRoom(Room room){
        Map<String, Object> map = new HashMap<String, Object>();
        if (roomService.updateRoom(room)>0){
            //成功
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"修改成功");
        }else {
            //失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"修改失败");
        }
        return map;
    }

    //删除房间
    @RequiresPermissions("room:delete")
    @RequestMapping("/deleteById")
    public Object deleteById(Integer id){
        Map<String, Object> map = new HashMap<String, Object>();
        if (roomService.deleteRoomById(id)>0){
            //成功
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"删除成功");
        }else {
            //失败
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"删除失败");
        }
        return map;
    }



}
